import { defineStore } from 'pinia'
import { apiGetUser } from '../apis/login'
export default defineStore(
  'userInfo',
  () => {
    const token = ref(null)
    const userInfo = ref({})
    const roles = ref([])
    const defaultAvatar = ref(
      'https://bpic.51yuansu.com/pic3/cover/04/26/65/66bd5a3c4686a_800.jpg?x-oss-process=image/sharpen,100'
    )
    const getUserInfo = async () => {
      userInfo.value = await apiGetUser()
      return userInfo.value
    }

    const logout = () => {
      uni.showModal({
        title: '提示',
        content: '确认退出登录？',
        success: function (res) {
          if (res.confirm) {
            token.value = null
            userInfo.value = {}
            uni._toPage('/pages/tabbar/home/index', 'tabbar')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        },
      })
    }
    return { token, userInfo, roles, getUserInfo, logout, defaultAvatar }
  },
  {
    unistorage: true, // 开启后对 state 的数据读写都将持久化
  }
)
